package com.hoyue.exam.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hoyue.classroom.bean.Classes;
import com.hoyue.classroom.bean.Course;
import com.hoyue.classroom.service.ClassesService;
import com.hoyue.classroom.service.CourseService;
import com.hoyue.common.utils.CoreConst;
import com.hoyue.common.utils.PageUtil;
import com.hoyue.common.utils.ResultUtil;
import com.hoyue.common.utils.ShiroUtils;
import com.hoyue.exam.bean.ExamQuestion;
import com.hoyue.exam.bean.Examination;
import com.hoyue.exam.service.ExamQuestionService;
import com.hoyue.exam.service.ExaminationService;
import com.hoyue.exam.vo.ExaminationConditionVo;
import com.hoyue.exam.vo.base.PageResultVo;
import com.hoyue.exam.vo.base.ResponseVo;
import com.hoyue.system.bean.User;
import com.hoyue.system.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("exam")
public class ExaminationController {

	@Autowired
	private ExaminationService examService;

	@Autowired
	private CourseService subjectService;

	@Autowired
	private UserService userService;

	@Autowired
	private ExamQuestionService examQuestionService;

	@Autowired
	private ClassesService classesService;

	@Autowired
	private CourseService courseService;


	@PostMapping("list")
	@ResponseBody
	public PageResultVo loadExam(ExaminationConditionVo examConditionVo, Integer limit, Integer offset) {
		examService.updateExamToStart();
		examService.updateExamToEnd();
//		User user = (User) SecurityUtils.getSubject().getPrincipal();
//		List<String> roleList = userService.selectRoleByUserId(user.getUserId());
//		if(!roleList.contains("超级管理员")) {
//			if(roleList.contains("老师")) {
//				examConditionVo.setAuthor(user.getNickname());
//			}else {
//				examConditionVo.setClassId(user.getClassId());
//			}
//		}
		PageHelper.startPage(PageUtil.getPageNo(limit, offset),limit);
		List<Examination> examList = examService.findByCondition(examConditionVo);
		PageInfo<Examination> pages = new PageInfo<>(examList);
		return ResultUtil.table(examList, pages.getTotal(), pages);
	}

	@GetMapping("/add")
	public String addExam(Model model) {
		Course course = new Course();
		course.setStatus(CoreConst.STATUS_INVALID);
		List<Course> courses = subjectService.selectCourses(course);
		List<String> grades = userService.selectGradeList();
		List<Classes> classes = classesService.selectAll();
		model.addAttribute("courses", courses);
		model.addAttribute("classes", classes);
		model.addAttribute("grades", grades);
		return "exam/publish";
	}


	@PostMapping("/add")
	@ResponseBody
	public ResponseVo add(Examination examination , Integer[]question) {
		try {
//			User user = (User) SecurityUtils.getSubject().getPrincipal();
//			examination.setUserId(user.getUserId());
//			examination.setAuthor(user.getNickname());
			User user = ShiroUtils.getUser();
			examination.setUserId(Integer.toString(user.getId()));
			examination.setAuthor(user.getUserName());
			Examination exam = examService.insertExam(examination);
			examQuestionService.insertList(exam.getId(),question);
			return ResultUtil.success("发布考试成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("发布考试失败");
		}
	}

	@GetMapping("/edit")
	public String edit(Model model, Integer id) {
		Examination examination = examService.selectById(id);
		model.addAttribute("examination", examination);
		Course course = new Course();
		course.setStatus(CoreConst.STATUS_INVALID);
		List<Course> courses = courseService.selectCourses(course);
		List<String> grades = userService.selectGradeList();
		List<Classes> classes = classesService.selectAll();
		model.addAttribute("courses", courses);
		model.addAttribute("classes", classes);
		model.addAttribute("grades", grades);
		List<ExamQuestion> examQuestions = examQuestionService.selectByExamId(id);
		List<Integer> questionIds = new ArrayList<>();
		for(ExamQuestion examQuestion : examQuestions) {
			questionIds.add(examQuestion.getQuestionId());
		}
		model.addAttribute("questionIds", questionIds);
		return "exam/detail";
	}

	@PostMapping("/edit")
	@ResponseBody
	public ResponseVo edit(Examination examination, Integer[]question) {
		examination.setStatus(0);
		examService.updateNotNull(examination);
		examQuestionService.removeByExamId(examination.getId());
		examQuestionService.insertList(examination.getId(),question);
		return ResultUtil.success("编辑考试成功");
	}

	@PostMapping("/delete")
	@ResponseBody
	public ResponseVo delete(Integer id) {
		int i = examService.deleteBatch(new Integer[] {id});
		int j = examQuestionService.deleteBatch(new Integer[] {id});
		if(i > 0 && j > 0) {
			return ResultUtil.success("删除考试成功");
		}else {
			return ResultUtil.error("删除考试失败");
		}
	}

	@PostMapping("/batch/delete")
	@ResponseBody
	public ResponseVo deleteBatch(@RequestParam("ids[]") Integer[] ids) {
		int i = examService.deleteBatch(ids);
		int j = examQuestionService.deleteBatch(ids);
		if(i > 0 && j > 0) {
			return ResultUtil.success("批量删除成功");
		}else {
			return ResultUtil.error("批量删除失败");
		}
	}

	
}